from django.shortcuts import render,HttpResponseRedirect
from log_reg.models import *
from order.models import *
from eat.models import *
from django.core.paginator import *
from django.db.models import F
from django.http import JsonResponse

order_ok_ = True

def order_F():
    global order_ok_
    order_ok_ = False

def order_T():
    global order_ok_
    order_ok_ = True

def order_ok(request):
    user = request.session.get('user')
    car_num = 0
    if user:
        user = User.objects.get(username=user)
        lingshi = user.order_lingshi_set.all()
        for i in lingshi:
            car_num += i.food_num
    global order_ok_
    return {'order_ok':order_ok_,'car_num':car_num}

def tuijian(request):
    food_tuijian = Eat_Food.objects.all().order_by('-food_sold')[:10]
    return {'food_tuijian':food_tuijian}

# 点餐
def eat_food(request):
    global order_ok_
    order_ok_ = True
    # 当前用户订单中数量
    user = request.session.get('user')
    order_num = False
    if user:
        user = User.objects.get(username=user)
        order_lingshi_all = user.order_lingshi_set.all()
        order_num = 0
        for i in order_lingshi_all:
            order_num += i.food_num
    # 模糊查询
    mohu = request.POST.get('mohu')
    if mohu == None:
        mohu = ''
    # 获取客户端传回的类别,默认为推荐类
    food_type = request.GET.get('food_type','新品上市')
    if food_type == '新品上市':
        food_all = Eat_Food.objects.filter(food_name__contains=mohu).order_by('-id')
    else:
        food_type = Food_type.objects.get(food_type=food_type)
        food_all = food_type.eat_food_set.filter(food_name__contains=mohu)

    # 防止页面显示的每个食品出现负数的BUG,将所有负数全部改为0
    for i in Eat_Food.objects.all():
        if i.food_number < 0:
            i.food_number = 0
            i.save()

    # 每页显示9条数据
    paginator = Paginator(food_all, 9)
    # 每次点击下一页/跳转所获取到的值
    page = request.GET.get('page')

    try:
        food_all = paginator.page(page)
    except PageNotAnInteger:
        # 如果传回的页数不是数字，则传递第一个页面。
        food_all = paginator.page(1)

    return render(request,'eat_food.html',{'food_all':food_all,'food_type':food_type,'order_num':order_num,'user':user})

def order_update(request):
    if request.method == "GET":
        if not request.session.get('user'):
            return HttpResponseRedirect('/logreg/log/')
        user = request.session.get('user')
        user = User.objects.get(username=user)
        # 存入
        # 获取食品的id
        food_id = request.GET.get('food_id')
        # 通过id获取对应的食品对象
        eat_food = Eat_Food.objects.get(pk=food_id)
        # 首先判断该用户下是否已经有该食品id,如果没有,就直接添加一条数据,如果有就在购买数量上+1
        food_lingshi = user.order_lingshi_set.filter(eat_food=food_id)
        if food_lingshi:
            # 防止在库存没有的情况下,以刷新页面的方式增加数量(此操作可以保证当前用户可购买的数量不超过库存量)
            if user.order_lingshi_set.get(eat_food=food_id).food_num < eat_food.food_number:
                food_lingshi.update(food_num=F('food_num') + 1)
        else:
            user.order_lingshi_set.create(img=eat_food.img, food_name=eat_food.food_name, food_money=eat_food.food_money,eat_food=food_id)
        return HttpResponseRedirect('/')


